package com.bed.lovewithpatientbed.service.impl;

import com.bed.lovewithpatientbed.common.bean.LayUiPageBean;
import com.bed.lovewithpatientbed.dao.HospitalManagerDao;
import com.bed.lovewithpatientbed.dao.WorkerMessageDao;
import com.bed.lovewithpatientbed.pojo.*;
import com.bed.lovewithpatientbed.service.DealerService;
import com.bed.lovewithpatientbed.service.HospitalManagerService;
import com.bed.lovewithpatientbed.service.HospitalService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.ref.WeakReference;
import java.util.Date;
import java.util.List;

/**
 * @author yg
 * @version 1.0.0
 * @ClassName HospitalService.java
 * @Description TODO
 */

@Service
@Transactional
public class HospitalManagerServiceImpl implements HospitalManagerService {

    @Resource
    private HospitalManagerDao hospitalManagerDao;

    @Resource
    private WorkerMessageDao workerMessageDao;

    @Resource
    private HospitalService hospitalService;
    @Resource
    private DealerService dealerService;


    @Override
    public List<HospitalManager> getAll() {
        return hospitalManagerDao.selectByExample(null);
    }

    @Override
    public List<HospitalManager> getHospitalManagerBySelect(LayUiPageBean pageBean, HospitalManager hospitalManager, Date staData , Date endDate) {

        HospitalManagerExample hospitalManagerExample=new HospitalManagerExample();
        HospitalManagerExample.Criteria criteria = hospitalManagerExample.createCriteria();

        //根据医院对接人名字模糊查询
        if(hospitalManager.getHospitalManagerName()!=null&&!"".equals(hospitalManager.getHospitalManagerName())){
            criteria.andHospitalManagerNameLike("%"+hospitalManager.getHospitalManagerName()+"%");
        }

        //根据医院对接人创建时间查询
        if(staData!=null&&!"".equals(staData)){
            criteria.andHospitalManagerDateGreaterThanOrEqualTo(staData);
        }
        if(endDate!=null&&!"".equals(endDate)){
            criteria.andHospitalManagerDateLessThanOrEqualTo(endDate);
        }

       //设置页面
        if(pageBean.getLimit() != null
                && !"".equals(pageBean.getLimit() )
                && pageBean.getPage() != null
                && !"".equals(pageBean.getPage())){
            hospitalManagerExample.setOffset((pageBean.getPage() - 1) * pageBean.getLimit());
            hospitalManagerExample.setLimit(pageBean.getLimit().intValue());
        }

        List<HospitalManager> hospitalManagerList = hospitalManagerDao.selectByExample(hospitalManagerExample);

        //设置工作信息表
        for (int i = 0; i <hospitalManagerList.size() ; i++) {
            hospitalManagerList.set(i,setClass(hospitalManagerList.get(i)));

        }

        return hospitalManagerList;
    }

    @Override
    public HospitalManager selectById(Integer hmid) {
        return hospitalManagerDao.selectByPrimaryKey(hmid);
    }

    @Override
    public int addHospitalManager(HospitalManager hospitalManager) {
        WorkerMessage workerMessage=new WorkerMessage();
        workerMessage.setWorkMessagePhone(hospitalManager.getHospitalManagerPhone());
        workerMessage.setWorkMessagePassword("123456");
        workerMessage.setRoleId(hospitalManager.getRole().getRoleId());
        workerMessage.setSoftdel(1);
        workerMessageDao.insertSelective(workerMessage);
        WorkerMessageExample example=new WorkerMessageExample();
        WorkerMessageExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(hospitalManager.getRole().getRoleId());
        criteria.andWorkMessagePhoneEqualTo(hospitalManager.getHospitalManagerPhone());
        List<WorkerMessage> workerMessages = workerMessageDao.selectByExample(example);
        hospitalManager.setWorkerMessageId(workerMessages.get(0).getWorkerMessageId());
        // emp
        hospitalManager.setSoftdel(1);
        return hospitalManagerDao.insertSelective(hospitalManager);
    }

    @Override
    public int delHospitalManager(HospitalManager hospitalManager) {
        hospitalManager.setSoftdel(0);
        return hospitalManagerDao.insert(hospitalManager);
    }

    @Override
    public int delbatchHospitalManager(HospitalManager[] hospitalManagers) {
        int countRow=0;
        if(hospitalManagers.length!=0){
            for (HospitalManager hospitalManager:hospitalManagers
                 ) {
                hospitalManager.setSoftdel(0);
                hospitalManagerDao.insert(hospitalManager);
                countRow+=1;
            }
        }
        return countRow;
    }

    @Override
    public int updateHospitalManager(HospitalManager hospitalManager) {
        return hospitalManagerDao.updateByPrimaryKeySelective(hospitalManager);
    }

    @Override
    public Integer getCount(HospitalManager hospitalManager, Date staData , Date endDate) {
        HospitalManagerExample hospitalManagerExample=new HospitalManagerExample();
        HospitalManagerExample.Criteria criteria = hospitalManagerExample.createCriteria();

        //根据医院对接人名字模糊查询
        if(hospitalManager.getHospitalManagerName()!=null&&!"".equals(hospitalManager.getHospitalManagerName())){
            criteria.andHospitalManagerNameLike("%"+hospitalManager.getHospitalManagerName()+"%");
        }

        //根据医院对接人创建时间查询
        if(staData!=null&&!"".equals(staData)){
            criteria.andHospitalManagerDateGreaterThanOrEqualTo(staData);
        }
        if(endDate!=null&&!"".equals(endDate)){
            criteria.andHospitalManagerDateLessThanOrEqualTo(endDate);
        }
        Long count = hospitalManagerDao.countByExample(hospitalManagerExample);
        return count.intValue();
    }

    /**
         * @title 根据电话判断登陆人是否为医院管理人
         * @description
         * @author yg
         * @updateTime
         * @throws
         */

    @Override
    public List<HospitalManager> isHospitalManager(String hospitalManagerPhone) {
        HospitalManagerExample hospitalManagerExample=new HospitalManagerExample();
        HospitalManagerExample.Criteria criteria = hospitalManagerExample.createCriteria();
        if (hospitalManagerPhone!=null&&!"".equals(hospitalManagerPhone)){
            criteria.andHospitalManagerPhoneEqualTo(hospitalManagerPhone);
        }
        List<HospitalManager> hospitalManagerList = hospitalManagerDao.selectByExample(hospitalManagerExample);

        return hospitalManagerList;
    }

    public HospitalManager setClass(HospitalManager hospitalManager){
        //加入员工信息
        hospitalManager.setWorkerMessage(workerMessageDao.selectByPrimaryKey(hospitalManager.getWorkerMessageId()));
        //加入负责人
        hospitalManager.setDealer(dealerService.selectDealer(hospitalManager.getDealerId()));
        return  hospitalManager;
    }

}
